import json
import random

from flask import Blueprint
from RootPath import RootPath
import os

from common.KafkaUtil import KafkaProduce
from common.YmlUtil import YmlUtil
from common.contactStr import mqttMessageContact, sqlContact

from common.RedisUtil import *
import numpy as np

from common.mysqlUtil import exc_sql

path = os.path.join(RootPath.getRootPath(), "Application.yml")
MySQLconf = YmlUtil().readYml(path,"mysql_test_90")

def realtimeGenerator():
    print(" =====realtimeGenerator=======")
    # 累计流量计1#
    point_value0 = {"No1CumulativeFlow": json.dumps(round(random.uniform(140000, 160000), 2))
                   # "Turbidity": json.dumps(round(random.uniform(2, 10), 2)),
                    #"ResidualChlorine": json.dumps(round(random.uniform(0.3, 0.8), 2))
                    }
    # 瞬时流量计1#
    point_value1 = {"No1InstantaneousFlow": json.dumps(round(random.uniform(3.5, 4), 2))
    #                 "Turbidity": json.dumps(round(random.uniform(2, 10), 2)),
    #                 "ResidualChlorine": json.dumps(round(random.uniform(0.5, 0.75), 2))
                   }
    # # 进水泵
    # jk_value = {"InstantaneousFlow": json.dumps(round(random.uniform(100, 800), 2)),
    #                 "CumulativeFlow": json.dumps(round(random.uniform(500, 50000), 2))
    #                 }
    # jk_value1 = {"No1InstantaneousFlow": json.dumps(round(random.uniform(100, 800), 2)),
    #                 "No1CumulativeFlow": json.dumps(round(random.uniform(500, 50000), 2))
    #                 }
    # jk_value2 = {"No2InstantaneousFlow": json.dumps(round(random.uniform(100, 800), 2)),
    #                 "No2CumulativeFlow": json.dumps(round(random.uniform(500, 50000), 2))
    #                 }
    # jk_value3 = {"No3InstantaneousFlow": json.dumps(round(random.uniform(100, 800), 2)),
    #              "No3CumulativeFlow": json.dumps(round(random.uniform(500, 50000), 2))
    #              }
    # jk_value4 = {"No4InstantaneousFlow": json.dumps(round(random.uniform(100, 800), 2)),
    #              "No4CumulativeFlow": json.dumps(round(random.uniform(500, 50000), 2))
    #              }
    #
    # # 出水泵
    #
    # ck_value = {"No5InstantaneousFlow": json.dumps(round(random.uniform(100, 800), 2)),
    #             "No5CumulativeFlow": json.dumps(round(random.uniform(500, 50000), 2))
    #             }
    # ck_value1 = {"No6InstantaneousFlow": json.dumps(round(random.uniform(100, 800), 2)),
    #              "No6CumulativeFlow": json.dumps(round(random.uniform(500, 50000), 2))
    #              }
    # ck_value2 = {"No7InstantaneousFlow": json.dumps(round(random.uniform(100, 800), 2)),
    #              "No7CumulativeFlow": json.dumps(round(random.uniform(500, 50000), 2))
    #              }
    # ck_value3 = {"No8InstantaneousFlow": json.dumps(round(random.uniform(100, 800), 2)),
    #              "No8CumulativeFlow": json.dumps(round(random.uniform(500, 50000), 2))
    #              }


    deviceID = ["11010001"]
    point_value = [point_value0]
    print("===========send datas==========")
    for i in range(len(deviceID)):
        # print("============mysql_hour==============")
        # sqldata_hour = sqlContact(deviceID[i], point_value[i], 'hour_')
        # exc_sql(sqldata_hour, MySQLconf)
        # print("============mysql_day==============")
        # sqldata_day = sqlContact(deviceID[i], point_value[i], 'day_')
        # exc_sql(sqldata_day, MySQLconf)
        print("============redis_realtimeData==============")
        value = mqttMessageContact(deviceID[i], point_value[i])
        KafkaProduce("DEV_DEVICE_DATA", value)
       # Redis().set_value("deviceKey:/device/" + deviceID[i], "/source", json.dumps(value))


